<template>
  <view class="edit-container">
    <view v-if="loading" class="loading">
      <uni-load-more status="loading"></uni-load-more>
    </view>
    <view v-else class="edit-form">
      <uni-forms :model="form" ref="form" :rules="rules" validate-trigger="submit" label-position="top">
        <uni-forms-item label="用户名称" name="userName" required>
          <uni-easyinput v-model="form.userName" placeholder="请输入用户名称" />
        </uni-forms-item>
        <uni-forms-item label="用户昵称" name="nickName" required>
          <uni-easyinput v-model="form.nickName" placeholder="请输入用户昵称" />
        </uni-forms-item>
        <uni-forms-item label="手机号码" name="phonenumber">
          <uni-easyinput v-model="form.phonenumber" placeholder="请输入手机号码" />
        </uni-forms-item>
        <uni-forms-item label="邮箱" name="email">
          <uni-easyinput v-model="form.email" placeholder="请输入邮箱" />
        </uni-forms-item>
        <uni-forms-item label="用户状态" name="status">
          <view class="status-switch">
            <text>停用</text>
            <switch :checked="form.status === '0'" @change="handleStatusChange" color="#13ce66" />
            <text>正常</text>
          </view>
        </uni-forms-item>
      </uni-forms>
      
      <view class="btn-group">
        <button type="primary" @click="submitForm">提交</button>
        <button type="default" @click="cancel">取消</button>
      </view>
    </view>
  </view>
</template>

<script>
import { getUser, updateUser, addUser } from "@/api/system/user";

export default {
  data() {
    return {
      loading: false,
      // 是否为编辑模式
      isEdit: false,
      // 表单参数
      form: {
        userId: undefined,
        userName: undefined,
        nickName: undefined,
        phonenumber: undefined,
        email: undefined,
        status: "0"
      },
      // 表单校验
      rules: {
        userName: {
          rules: [
            { required: true, errorMessage: '请输入用户名称' }
          ]
        },
        nickName: {
          rules: [
            { required: true, errorMessage: '请输入用户昵称' }
          ]
        },
        phonenumber: {
          rules: [
            { pattern: /^1[3-9]\d{9}$/, errorMessage: '请输入正确的手机号码' }
          ]
        },
        email: {
          rules: [
            { pattern: /^\S+@\S+\.\S+$/, errorMessage: '请输入正确的邮箱地址' }
          ]
        }
      }
    };
  },
  onLoad(options) {
    if (options && options.userId) {
      this.isEdit = true;
      this.form.userId = options.userId;
      this.getUser();
    } else {
      this.isEdit = false;
    }
  },
  methods: {
    /** 获取用户详细信息 */
    getUser() {
      this.loading = true;
      getUser(this.form.userId).then(response => {
        this.form = response.data;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.form.validate().then(res => {
        this.loading = true;
        if (this.isEdit) {
          updateUser(this.form).then(res => {
            this.$modal.showSuccess("修改成功");
            this.loading = false;
            this.cancel();
          }).catch(() => {
            this.loading = false;
          });
        } else {
          addUser(this.form).then(res => {
            this.$modal.showSuccess("新增成功");
            this.loading = false;
            this.cancel();
          }).catch(() => {
            this.loading = false;
          });
        }
      }).catch(errors => {
        this.$modal.showToast(errors[0].errorMessage);
      });
    },
    /** 取消按钮 */
    cancel() {
      uni.navigateBack();
    },
    /** 切换状态 */
    handleStatusChange(e) {
      this.form.status = e.detail.value ? '0' : '1';
    }
  }
};
</script>

<style lang="scss" scoped>
.edit-container {
  padding: 20px;
}

.loading {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
}

.edit-form {
  background-color: #fff;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.status-switch {
  display: flex;
  align-items: center;
  
  text {
    margin: 0 10px;
    color: #606266;
  }
}

.btn-group {
  display: flex;
  justify-content: center;
  margin-top: 30px;
  
  button {
    margin: 0 10px;
    width: 120px;
  }
}
</style>